<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tests extends Backend_Controller {

	protected $_title = 'Ujian / Ulangan';
	
	public function __construct() {
		parent::__construct();
		if(parent::get_actor_login()!="student")
			redirect('login/index/1');
		$this->load->model('school_years_model', 'school_years', true);
		$this->load->model('users_model', 'users', true);
		$this->load->model('tests_model', 'tests', true);
		$this->load->model('test_question_model', 'test_question', true);
		$this->load->model('test_question_student_model', 'test_question_student', true);
		$this->load->model('test_student_skip_model', 'test_student_skip', true);
		$this->load->model('subjects_model', 'subjects', true);
	}
		
	public function index() {
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		
		$rs = $this->school_years->get_active_school_year();
		$student = $this->users->get_student($_SESSION['user']);
		$tests = $this->tests->get_tests($rs[0]['id'], $student['class_id']);
		
		$data = array(
			'mainview' => 'tests_view',
			'tests'=>$tests,
		) + $this->_get_header_data();
		
		$this->load->view('student/template',$data);
	}
	
	public function start_test($test_id, $error=0){
		
		
		$rs = $this->school_years->get_active_school_year();
		$student = $this->users->get_student($_SESSION['user']);
		$test = $this->tests->get_tests($rs[0]['id'], $student['class_id'], $test_id);
		//$id = $this->tests->start_duration($test_id, $test[0]['duration']);
		$start_time = $this->tests->get_start_time($test_id);
		$last_number = $this->test_question_student->get_last_number($test_id, $_SESSION['user']);
		
		//if($error==0) {
			$_SESSION['test_id'] = $test[0]['id'];
			$_SESSION['question_id'] = $test[0]['question_id'];
			$_SESSION['percentage'] = $test[0]['percentage'];
			$_SESSION['total_qty'] = $test[0]['total_qty'];
			$_SESSION['total_skip_test'] = $test[0]['maximum_skip'];
			$_SESSION['total_skip_student'] = 0;
			$_SESSION['problem_per_page']=1;
			$_SESSION['no'] = $last_number;
			$_SESSION['random_number'] = 0;
		//}
			/*
			if($id>0 && isset($_SESSION['test_duration_id']))
				if($_SESSION['test_duration_id'] =='' || $_SESSION['test_duration_id'] != $id)
					$_SESSION['test_duration_id'] =$id;
			*/
			
			//if(!isset($_SESSION['duration']))
			//	$_SESSION['duration'] = $test[0]['duration']*60;
			//else
				$_SESSION['duration'] = ($test[0]['duration']*60) - (strtotime(date('H:i:s')) - strtotime($start_time));
		
		$data = array(
			'mainview' => 'start_test_view',
			'test' => $test,
			'start' => $start_time
		) + $this->_get_header_data();
		
		if($_SESSION['duration']>0)
			$this->load->view('student/template',$data);
		else{
			$data['mainview'] = 'test_done';
			$this->load->view('student/template',$data);
		}
		
	}

	public function ajax(){
		$total_skip_test = $_SESSION['total_skip_test'];
		$total_skip_student = $this->test_student_skip->get_total_skip_student($_SESSION['test_id'], $_SESSION['user']);
		
		if( $this->test_question_student->is_done($_SESSION['test_id'], $_SESSION['user']) )
		{
			$_SESSION['duration'] = 0;
			echo "selesai";
		}	
		
		else 
		{
			
			$questions = $this->test_question->get_questions($_SESSION['test_id']);
		
			//display problem set
			$total_qty = $_SESSION['total_qty'];
			$random_number = rand(0,$total_qty-1);
			
			if($total_skip_student < $total_skip_test)
				$_SESSION['random_number'] = $random_number;
				
			$last_number = $this->test_question_student->get_last_number($_SESSION['test_id'], $_SESSION['user']);
			
			$_SESSION['no'] = $last_number;
			$_SESSION['question_detail_id'] = $questions[ $_SESSION['random_number'] ]['question_detail_id'];
			
			$retval = "<ol start=" . $_SESSION['no']. ">";
					
			//clear <p></p> HTML tag from text editor
			//edited: 7/5/2014; non aktifkan 3 baris di bawah
			//$search = array("<p>","</p>");
			//for($a=97; $a<=101; $a++)
				//$questions[$_SESSION['random_number']]['option_' . chr($a)] = str_replace($search, "",$questions[ $_SESSION['random_number'] ]['option_' . chr($a)]);
			
			//comment perintah dibawah ini per 05 Mei 2014 09:03 AM.
			//$questions[$_SESSION['random_number']]['problem'] = str_replace($search,"",$questions[$_SESSION['random_number']]['problem']);
			
			
			$is_exist = $this->test_question_student->is_exist($_SESSION['question_detail_id'],$_SESSION['user'], $_SESSION['test_id']); //jika pertanyaan sudah pernah dijawab
			if($is_exist){
				$random_number = rand(0,$total_qty-1);
				$_SESSION['random_number'] = $random_number;
				$this->ajax();
			}
			else
			{
			
				//$retval .= var_dump($questions) . "<Br>". $random_number . " - " . $total_qty . " = " . $questions[$_SESSION['no']]['problem'];
				//$retval .= $_SESSION['question_detail_id'] . " = " . $questions[$_SESSION['no']]['problem'];
				$retval .= "<li> " . $questions[$_SESSION['random_number']]['problem'] . "</li> </ol>" ;
				
				for($a=97; $a<=101; $a++)
					$retval .= "<Br>" .  chr($a-32) .". <input type=radio name=$_SESSION[question_detail_id] value='" . chr($a-32). "'>" . $questions[$_SESSION['random_number']]['option_'. chr($a)];
				
				echo $retval;
			}
			
		}
		
	}
	
	public function skip(){
		$total_skip_test = $_SESSION['total_skip_test'];
		$total_skip_student = $this->test_student_skip->get_total_skip_student($_SESSION['test_id'], $_SESSION['user']);
		if($total_skip_student < $total_skip_test){
			if($total_skip_student ==0)
				$this->test_student_skip->insert_total_skip_student($_SESSION['test_id'], $_SESSION['user'], $total_skip_student+1);
			else
				$this->test_student_skip->update_total_skip_student($_SESSION['test_id'], $_SESSION['user'], $total_skip_student+1);
			$_SESSION['total_skip_student'];
		}
		$this->ajax();
	}
	
	public function gettotalskip(){
		$total_skip_student = $this->test_student_skip->get_total_skip_student($_SESSION['test_id'], $_SESSION['user']);
		//return $total_skip_student;
		echo $total_skip_student;
	}
	
	public function done(){
		$this->tests->clean_duration($_SESSION['test_duration_id']);
		$this->tests->stop_test($_SESSION['test_id']);
		redirect("student/landing");
	}
	
	public function save() {
		$save_test = $this->input->post('submit');
		if (!empty($save_test)) {
		
			$this->form_validation->set_rules($_SESSION['question_detail_id'], 'Pilihan Jawaban', 'required');

			if ($this->form_validation->run() == FALSE)
			{
				$rs = $this->school_years->get_active_school_year();
				$student = $this->users->get_student($_SESSION['user']);
				$test = $this->tests->get_tests($rs[0]['id'], $student['class_id'], $_SESSION['test_id']);
				$start_time = $this->tests->get_start_time($_SESSION['test_id']);
				
				$data['mainview'] = 'start_test_view';
				$data['test'] = $test;
				$data['start'] = $start_time;
				
				$this->load->view('student/template',$data);
				
			}
			else
			{
				$student_answer = $this->input->post($_SESSION['question_detail_id']);
				if($student_answer != ""){
					$this->test_question_student->insert($this->input);
					$this->session->set_flashdata('info', "Data berhasil ditambahkan.");
					if( $this->test_question_student->is_done($_SESSION['test_id'], $_SESSION['user']) )
						redirect('student/landing');
					else
						redirect('student/tests/start_test/' . $_SESSION['test_id']. '/1');
					
					$_SESSION['random_number'] = rand(0,$_SESSION['total_qty']-1);
					
				}
			}
		}
	}
}
